(function () {
	
    //人员table数据显示
    var serverData = {};
    serverData.cyc = 'week';
    serverData.beginDate = '';
    serverData.endDate = '';
    var	serverTable;
    
    function viewDetails(o){
    		window.location = window.rootPath + '/ui/eventRegister?eventReportIdentify='+o.registerEventToReportEvent.id+"&eventRegisterIdentify="+o.id;
    }
    
    jQuery(document).ready(function () {
        serverTable=  jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "post",
                "cache": false,
                "url": window.rootPath+'/ui/EventAnalysis/getEventPage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [{
	                data: null,
	                defaultContent: ""
        			},
        			{
					data : 'registerEventAccTime',
					render : function(data,type, full, meta) {
						if (data)
							return moment(data).format('YYYY/MM/DD');
						else
							return '';
					}
				},
                {
                		data: 'registerEventAdress'
                },
                {
                		data: 'registerEventType.eventName'
                },
                {
                		data: 'registerEventState.title'
                },
                {
                		data: 'registerEventToNetGrid.gridName'
                },
                {
                		data: 'registerEventCreateUser.name'
                }],
            "drawCallback": function ( oSettings ) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
                {
                    $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
                    k++;
                }

                serverTable.off('click','tr');
                serverTable.on( 'click', 'tr', function (event) {
                    event=event||window.event;
                    var target=event.target||event.scrElement;
                    var data = serverTable.row( this ).data();
                    return;
                } );
            }
            
        });
        
        getAnalysisData();
        
        //人员查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#search').val();
            serverTable.draw();
        })
        
        //人员撤销查询
        jQuery('.resetBtn').on("click",function(){
            jQuery('#search').val('');
            serverData.key ='';
            serverTable.draw();
        });
        
        //周期切换
        jQuery('#cyc').on('change',function(){
        		var cyc = $(this).val();
        		var cycDate = $('#cycDate');
        		serverData.cyc = cyc;
        		if(cyc == 'custom'){
        			cycDate.show();
        		}else{
        			cycDate.hide();
        			serverTable.draw(false);
        			getAnalysisData();
        		}
        })
        
        jQuery('.cycBtn').on("click",function(){
        		var beginDate = $('#beginDate').val();
        		var endDate = $('#endDate').val();
        		if(beginDate == '' || endDate == ''){
        			alert("请选择周期时间");
        			return;
        		}
        	    serverData.beginDate = beginDate;
        	    serverData.endDate = endDate;

            serverTable.draw(false);
            getAnalysisData();
        });
        
    });
    
    function getAnalysisData(){
    		var d = {
    				'cyc' : $('#cyc').val(),
    				'beginDate' : $('#beginDate').val(),
    				'endDate' : $('#endDate').val()
    			}
	    jQuery.ajax({
	        url:window.rootPath+'/ui/EventAnalysis/getAnalysisData/',
	        cache:false,
	        type :"post",
	        data :JSON.stringify(d),
	        dataType:"json",
	        contentType:'application/json;charset=utf-8',//解决返回数据中的中文乱码问题
	        success:function(data){
	        		var grid = data.grid;
	        		updateAxis1(grid);
	        		var event = data.event;
	        		updatePie1(event);
	        		var date = data.date;
	        		updateAxis2(date);
	        		var status = data.status;
	        		updatePie2(status);
	        }
	    });
    };
    
    function updateAxis1(data){
    		axis1.setOption({
    			legend:{ data : data.legend },
    			xAxis:{ data : data.xAxis },
    			series: data.series
    		});
    }
    function updateAxis2(data){
	    	axis2.setOption({
	    		legend:{ data : data.legend },
	    		xAxis:{ data : data.xAxis },
	    		series: data.series
	    	});
    }
    
    function updatePie1(data){
    		pie1.setOption({
			legend:{ data : data.legend },
			series:{ data : data.series }
		});
    }
    
    function updatePie2(data){
	    	pie2.setOption({
	    		legend:{ data : data.legend },
	    		series:{ data : data.series }
	    	});
    }

    
    /**
     * 按网格进行分类
     */
    var axis1;
    axis1 = echarts.init(document.getElementById('axis1'));
    
    var axisOpt1 = {
    	    title: {
    	        text: '案件地点分析'
    	    },
    	    tooltip: {
    	        trigger: 'axis',
    	        axisPointer: {
    	            type: 'shadow'
    	        }
    	    },
    	    legend: {
    	        data: ["交通堵", "环境脏", "秩序乱"]
    	    },
    	    grid: {
    	        left: '3%',
    	        right: '4%',
    	        bottom: '3%',
    	        containLabel: true
    	    },
    	    xAxis: {
    	        type: 'category',
    	        position: "bottom",
    	        axisLabel :{
	            interval:0,
	            rotate:-10
            },
    	        data: ['其他']
    	    },
    	    yAxis: {
    	        type: 'value',
    	        boundaryGap: [0, 0.01]
    	    },
    	    series: [
    	        {
    	            name: '交通堵',
    	            type: 'bar',
    	            stack: 'group',
    	            data: []
    	        },
    	        {
    	            name: '环境脏',
    	            type: 'bar',
    	            stack: 'group',
    	            data: []
    	        },
    	        {
    	            name: '秩序乱',
    	            type: 'bar',
    	            stack: 'group',
    	            data: []
    	        }
    	    ]
    	};
    
    axis1.setOption(axisOpt1);
    
    /**
     * 按案件类型进行分类
     */
    var pie1;
    pie1 = echarts.init(document.getElementById('pie1'));
    var pieOpt1 = {
    	    title: {
    	        text: '事件类型分析',
    	    },
    	    tooltip: {
    	        trigger: 'item',
    	        formatter: function(params, ticket, callback) {

    	            var res = params.name + ' : ' + params.value + '</br>' 
    	            		+ params.percent +'%';
    	            return res;
    	        }
    	    },
    	    legend: {
    	        data: ["交通堵", "环境脏", "秩序乱"],
    	    },
    	    series: {
    	        name: '事件类型',
    	        type: 'pie',
    	        radius: '68%',
    	        center: ['50%', '50%'],
    	        clockwise: false,
    	        data: []
    	    }
    	};
    pie1.setOption(pieOpt1);
    
    /**
     * 按时间进行分类
     */
    var axis2;
    axis2 = echarts.init(document.getElementById('axis2'));
    
    var axisOpt2 = {
    	    title: {
    	        text: '案件时间分析'
    	    },
    	    tooltip: {
    	        trigger: 'axis',
    	        axisPointer: {
    	            type: 'shadow'
    	        }
    	    },
    	    legend: {
    	        data: ["交通堵", "环境脏", "秩序乱"]
    	    },
    	    grid: {
    	        left: '3%',
    	        right: '4%',
    	        bottom: '3%',
    	        containLabel: true
    	    },
    	    xAxis: {
    	        type: 'category',
    	        position: "bottom",
    	        axisLabel :{
	            interval:0,
	            rotate:-10
            },
    	        data: ['其他']
    	    },
    	    yAxis: {
    	        type: 'value',
    	        boundaryGap: [0, 0.01]
    	    },
    	    series: [
    	        {
    	            name: '交通堵',
    	            type: 'bar',
    	            stack: 'group',
    	            data: []
    	        },
    	        {
    	            name: '环境脏',
    	            type: 'bar',
    	            stack: 'group',
    	            data: []
    	        },
    	        {
    	            name: '秩序乱',
    	            type: 'bar',
    	            stack: 'group',
    	            data: []
    	        }
    	    ]
    	};
    
    axis2.setOption(axisOpt2);
    
    /**
     * 按状态类型进行分类
     */
    var pie2;
    pie2 = echarts.init(document.getElementById('pie2'));
    var pieOpt2 = {
    	    title: {
    	        text: '事件状态分析',
    	    },
    	    tooltip: {
    	        trigger: 'item',
    	        formatter: function(params, ticket, callback) {

    	            var res = params.name + ' : ' + params.value + '</br>' 
    	            		+ params.percent +'%';
    	            return res;
    	        }
    	    },
    	    legend: {
    	        data: ["交通堵", "环境脏", "秩序乱"],
    	    },
    	    series: {
    	        name: '事件类型',
    	        type: 'pie',
    	        radius: '68%',
    	        center: ['50%', '50%'],
    	        clockwise: false,
    	        data: []
    	    }
    	};
    pie2.setOption(pieOpt2);
    
})();